﻿$(document).ready(function () {
    $.ajax({
        type: "post",
        data: { action: "GetList" },
        dataType: "json",
        url: "../../Ajax/PrizeTypeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = data.json;
                var tbody = "";
                $.each(json, function (k, v) {
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblID' id='" + v.ID + "'>";
                    tbody += v.ID;
                    tbody += "</label></td>"
                    tbody += "<td><lable name='lblName'>";
                    tbody += v.Name;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIsVirtual' >";
                    tbody += v.IsVirtual;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblRemark' >";
                    tbody += decodeURIComponent(v.Remark);
                    tbody += "</label></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });

    $("#btnAdd").click(function () {
        Edit(null);
    })
})

function Delete(contr) {
    var id = $(contr).parent().parent().find("lable[name=lblID]").first().text();
    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: id },
            async: true,
            url: "../../Ajax/PrizeTypeAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $($(contr).parent().parent()).remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}

function Update(contr) {
    Edit(contr);
}

function Edit(contr) {
    var id = $(contr).parent().parent().find("lable[name=lblID]").first().text();
    var name = $(contr).parent().parent().find("lable[name=lblName]").first().text();
    var remark = $(contr).parent().parent().find("lable[name=lblRemark]").first().text();
    var IsVirtual = $(contr).parent().parent().find("lable[name=lblIsVirtual]").first().text();
    if (contr != null) {
        content = "<table><tr><td>物品类型名称：<input type='text' id='txtName' value='" + name + "' style='width:200px;float:right' /></td></tr>";
        if (IsVirtual == "True")
            content += "<tr><td>是否货币：<input type='radio' name='radioIsVirtual' id='radioisTure' value='True' checked='checked' /><lable>是</lable><input type='radio' name='radioIsVirtual' id='radioisfalse' value='False'/><lable>否</lable></td></tr>";
        else
            content += "<tr><td>是否货币：<input type='radio' name='radioIsVirtual' id='radioisTure' value='True'/><lable>是</lable><input type='radio' name='radioIsVirtual' id='radioisfalse' checked='checked'  value='False'/><lable>否</lable></td></tr>";
        content += "<tr><td>物品类型说明：<textarea  cols='50' rows='10' id='txtRemark' style='width:198px;float:right'>" + remark + "</textarea></td></tr></table>";
    } else
        content = "<table><tr><td>物品类型号：<input type='text' id='txtID' style='width:200px;float:right' /></td></tr>"
                + "<tr><td>物品类型名称：<input type='text' id='txtName' value='' style='width:200px;float:right' /></td></tr>"
                + "<tr><td>是否货币：<input type='radio' name='radioIsVirtual' id='radioisTure' value='True' checked='checked' /><lable>是</lable><input type='radio' name='radioIsVirtual' id='radioisfalse' value='False'/><lable>否</lable></td></tr>"
                + "<tr><td>物品类型说明：<textarea  cols='50' rows='10' id='txtRemark' style='width:198px;float:right'></textarea></td></tr></table>";
    $.dialog({
        title: "物品类型模板",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            var objID = $("#txtID").val();
                            var objName = $("#txtName").val();
                            var objRemark = $("#txtRemark").val();
                            if (contr == null && $.trim(objID) == "") {
                                $.dialog.alert("请输入物品类型号！");
                                return false;
                            }
                            if ($.trim(objName) == "") {
                                $.dialog.alert("请输入物品类型名称！");
                                return false;
                            }
                            AddMould(contr, $("#txtName"));
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}

function AddMould(contr, txtName) {
    var id = contr == null ? $(txtName).parent().parent().parent().find("#txtID").val() : $(contr).parent().parent().find("lable[name=lblID]").first().text();
    var name = $(txtName).val();
    var remark = $(txtName).parent().parent().parent().find("#txtRemark").val();
    var IsVirtual = $(txtName).parent().parent().parent().find("input[name=radioIsVirtual]:checked").val();

    var data = contr == null ? { action: "Add", id: id, name: name, remark: remark, IsVirtual: IsVirtual} :
              { action: "Update", id: id, name: name, remark: remark, IsVirtual: IsVirtual };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/PrizeTypeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data == "True") {
                $.dialog.tips("更新成功！", 1);
                if (contr == null) {
                    var tbody = "<tr>";
                    tbody += "<td><lable name='lblID' id='" + id + "'>";
                    tbody += id;
                    tbody += "</label></td>"
                    tbody += "<td><lable name='lblName'>";
                    tbody += name;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIsVirtual' >";
                    tbody += IsVirtual;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblRemark' >";
                    tbody += remark;
                    tbody += "</label></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>";
                    tbody += "</td>";
                    tbody += "</tr>"
                    $("#tb1").append(tbody);
                }
                else {
                    $(contr).parent().parent().find("lable[name=lblName]").first().text(name);
                    $(contr).parent().parent().find("lable[name=lblRemark]").first().text(remark);
                    $(contr).parent().parent().find("lable[name=lblIsVirtual]").first().text(IsVirtual);
                }
            }
            else {
                $.dialog.tips("更新失败！", 1);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}